package com.example.auth.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.auth.model.MenuModel;
import com.example.auth.model.UserModel;
import com.example.auth.service.MenuModelService;
import com.example.db.SqlBuilder;
import com.example.db.TableService;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @Classname MenuModelServiceImpl
 * @Description TODO
 * @Date 2022/7/29 11:13
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class MenuModelServiceImpl implements MenuModelService {

    @Autowired
    private TableService tableService;

    @Override
    public void updateMenuModelById(MenuModel menuModel) {
        tableService.updateById(menuModel,MenuModel.class);
    }

    @Override
    public void saveMenuModel(MenuModel menuModel) {
        tableService.save(menuModel,MenuModel.class);
    }

    @Override
    public List<MenuModel> getMenuModels() {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(MenuModel.class);
        List<MenuModel> menuModels=tableService.selectList(sqlBuilder,MenuModel.class);
        return menuModels;
    }

    @Override
    public MenuModel getMenuModelById(Object id) {
        MenuModel menuModel=tableService.getOneById(id,MenuModel.class);
        return menuModel;
    }

    @Override
    public List<MenuModel> getMenuModelsByUid(String uid) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(MenuModel.class);
        List<MenuModel> menuModels=tableService.selectList(sqlBuilder,MenuModel.class);

        List<MenuModel> menuModelsTree=menuModels.stream().filter(e->{
           return e.getPid()==0 || e.getPid()==null;
        }).map((m)->{
            m.setChild(getChilds(menuModels,m));
            return m;
        }).collect(Collectors.toList());
        return menuModelsTree;
    }


    @Override
    public List<MenuModel> getMenuModelsByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return Collections.emptyList();
        }
        String str= JSONArray.toJSONString(ids);
        List<String> idStringList=JSONArray.parseArray(str,String.class);
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(MenuModel.class);
        sqlBuilder.and("id").in(idStringList);
        List<MenuModel> menuModels = tableService.selectList(sqlBuilder,MenuModel.class);
        if(menuModels==null || menuModels.isEmpty()){
            return menuModels;
        }
        List<MenuModel> menuModelsTree=menuModels.stream().filter(e->{
            return e.getPid()==0 || e.getPid()==null;
        }).map((m)->{
            m.setChild(getChilds(menuModels,m));
            return m;
        }).collect(Collectors.toList());
        return menuModelsTree;
    }

    /**
     * 处理树结构
     * @param
     * @return
     */
    private List<MenuModel> getChilds(List<MenuModel> menuModelsAll,MenuModel menuModel){
        List<MenuModel> menuList=menuModelsAll.stream().filter(e->{
            return Objects.equals(e.getPid(),menuModel.getId());
        }).map(
                (m)->{
                    m.setChild(getChilds(menuModelsAll,m));
                    return m;
                }
        ).collect(Collectors.toList());
        return menuList;
    }
}
